<?php

namespace backend\models\service;

use Yii;
use yii\data\Sort;
use backend\models\odm\NovelChargeOdm;
use backend\models\service\NovelService as Novel;

class Charge extends NovelChargeOdm {

    private static $_items = [];

    public static function items($id) {
        if (!isset(self::$_items[$id])) {
            self::loadItems($id);
        }
        return self::$_items[$id];
    }

    private static function loadItems($id) {
        self::$_items[$id] = [];
        $models = static::find()->where(['chapter_novel_id' => $id])->orderBy(['chapter_order' => SORT_ASC])->asArray()->all();

        foreach ($models as $model) {
            self::$_items[$id][$model['chapter_id']] = $model['chapter_name'];
        }
    }

//    public function getChargeInfo($novel_id) {
//        $sql = "select * from xs_novel_charge where novel_id = " . $novel_id;
//        $info = Yii::$app->db->createCommand($sql)->queryOne();
//        return $info;
//    }
//    
//    public function upChargeStatus($novel_id,$status) {
//        $sql = "update  xs_novel_charge set charge_status = " .$status. " where novel_id = " . $novel_id;
//        $res = Yii::$app->db->createCommand($sql)->execute();
//        return $res;
//    }
    
    /**
     * 获取收费标准by id
     */
    
    public function getChargeModelById($charge_id){
        $sql = "select * from xs_novel_charge where charge_id = " .$charge_id;
        $res = Yii::$app->db->createCommand($sql)->queryOne();
        return $res;
    }
    
    /**
     * 获取收费标准by model
     */
    
    public function getChargeModel($model){
        $sql = "select * from xs_novel_charge where charge_model = " .$model;
        $res = Yii::$app->db->createCommand($sql)->queryOne();
        return $res;
    }
    
    
    /**
     * 获取收费标准
     */
    
    public function getChargeModelInfo($s,$p){
        $s  = !empty($s) ? $s: 0;
        $sql = "select * from xs_novel_charge where charge_start_chapter = " .$s." and charge_price = ".$p;
        $res = Yii::$app->db->createCommand($sql)->queryOne();
        return $res;
    }
    
    /**
     * 保存收费标准 'charge_model' => $request['charge_model'],
                        'charge_start_chapter' => $request['charge_start_chapter'],
                        'charge_price' => $request['charge_price'],
                        'novel_create_time' => date('Y-m-d H:i:s')
     */
    public function addChargeModel($data , $type = 0){
        $charge_model = $data['charge_model'];
        $charge_start_chapter  = !empty($data['charge_start_chapter']) ? $data['charge_start_chapter'] : 0;
        $charge_price = $data['charge_price'];
        $create_time = $data['novel_create_time'];
        $sql = "insert into xs_novel_charge (`charge_model`,`charge_start_chapter`,`charge_price`,`novel_create_time`) values($charge_model,$charge_start_chapter,$charge_price"
                . ",'$create_time')" ;
        $res = Yii::$app->db->createCommand($sql)->execute();
        if($res){
            $id = Yii::$app->db->getLastInsertID();
            if($type == 1){
                return $id;
            }
            $novel = new Novel();
            $res = $novel->upChargeStatus($data['novel_id'], $id);
        }
        return $res;
        
    }

}
